package com.imay.live.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.PermissionChecker;

import com.imay.live.R;
import com.imay.live.activity.BasePermissionActivity;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017/1/20.
 */
public class PermissionUtil {
    public static boolean selfPermissionGranted(Context context, String permission) {
        // For Android < Android M, self permissions are always granted.
        boolean result = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (getTargetSdkVersion(context) >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                result = context.checkSelfPermission(permission)
                        == PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                result = PermissionChecker.checkSelfPermission(context, permission)
                        == PermissionChecker.PERMISSION_GRANTED;
            }
        }
        return result;
    }

    private static int getTargetSdkVersion(Context context) {
        int version = 0;
        try {
            final PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            version = info.applicationInfo.targetSdkVersion;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();

        }
        return version;
    }

    private static final int REQUEST_CODE_PERMISSION = 101;

    /**
     * 申请读写权限
     *
     * @param context
     * @param permissionListener
     */
    public static void requestStoragePermission(final Context context, PermissionListener permissionListener) {
        List<String> deniedPermissions = new ArrayList<>(Arrays.asList(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE));
        if (AndPermission.hasAlwaysDeniedPermission(context, deniedPermissions)) {
            AndPermission.with(context)
                    .requestCode(REQUEST_CODE_PERMISSION)
                    .permission(Permission.STORAGE)
                    .callback(permissionListener).rationale(new RationaleListener() {
                @Override
                public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                    // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                    AndPermission.rationaleDialog(context, rationale).show();
                }
            }).start();
        }
    }

    /**
     * 弹出读写权限提示框
     *
     * @param context
     * @param deniedPermissions
     */
    public static void showStoragePermissionDialog(final Context context, @NonNull List<String> deniedPermissions) {
        if (AndPermission.hasAlwaysDeniedPermission(context, deniedPermissions)) {
            AndPermission.defaultSettingDialog(context)
                    .setTitle(R.string.permission_sd_title)
                    .setMessage(R.string.permission_sd)
                    .setPositiveButton(R.string.permission_button_setting)
                    .show();
        }
    }
}
